package com.swxy.controller;

import com.swxy.pojo.User;
import com.swxy.pojo.admin;
import com.swxy.service.LoginService;
import com.swxy.service.adminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class logon1Controller {
    @Autowired
    LoginService userService;
    @Autowired
    adminService adminService;

    @RequestMapping(value = "/search", method = RequestMethod.POST)
//    public String login(@RequestParam("phone") String phone, @RequestParam("password") String password, HttpSession session) {
//        User user = userService.login(phone,password);
//        if (user != null) {
//            session.setAttribute("user", user);
//            return "usersuccess";
//        } else {
//            admin admin = adminService.adminLogin(phone,password);
//            if (admin != null) {
//                // 将管理员信息存入session中
//                session.setAttribute("admin", admin);
//                return "success";
//            } else {
//                return "no"; //
//            }
//        }

    public ModelAndView search(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        ModelAndView mav = new ModelAndView();

      User users = userService.login(phone,password);

       admin admins = adminService.adminLogin(phone,password);
        if (users!=null) {
            mav.addObject("users", users);
            mav.setViewName("usersuccess");
        } else if (admins!=null) {
            mav.addObject("admins", admins);
            mav.setViewName("/jsp/user.jsp");
        } else {
            mav.addObject("errorMsg", "No results found!");
            mav.setViewName("no");
        }
        return mav;

    }
}
